/**
 * 
 */
package com.qiaoan.interceptor;

import com.qiaoan.back.model.sys.UserModel;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zhang_chong521
 *
 */
public class OAuthInterceptor implements HandlerInterceptor {

	private static Logger logger = LogManager.getLogger(OAuthInterceptor.class.getName());
	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

		logger.info("*************请求进入认证拦截器***************");
		UserModel user = (UserModel) request.getSession().getAttribute("session_user");
		if(user == null){//未登录
			
			String URL = request.getRequestURL().toString();
			if (URL.indexOf("/Blogin/togo") != -1 || URL.indexOf("/flogin/togo") != -1 || URL.indexOf("/mlogin/togo") != -1) {
	                return true;
	            }
            // 请求的路径
            String contextPath = request.getContextPath();
            response.sendRedirect(contextPath + "/login/togo");
            return false ;
			
		}else{
			
			return true ;
		}
	}

}
